package com.gzy.gameserver;

import java.sql.ResultSet;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.sql.ResultSet;
import com.gamalocus.sgs.services.mysql.MySQLManager;
import com.sun.sgs.app.AppContext;


public class MagicManager 
{
	public static final byte Magic_Normal = 0;


	protected Map<Integer, Magic.MagicData> magicMap = new HashMap<Integer, Magic.MagicData>();

	private static final MagicManager s_magicMgr = new MagicManager();
	
	private static final Logger logger = Logger.getLogger(Pawn.class.getName());
	
	public MagicManager()
	{
		this.initialize();
	}
	
	public static MagicManager sharedManager()
	{
		return s_magicMgr;
	}
	
	public Magic spawnMagic(int id)
	{
		Magic.MagicData magicData = this.magicMap.get(new Integer(id));
		if (magicData.m_type == Magic_Normal)
		{
			Magic magic = new Magic();
			if (magic != null)
			{
				magic.set_id(magicData.m_id);
				magic.set_type(magicData.m_type);
				magic.set_attack_power(magicData.m_attack_power);
				magic.set_start_time(magicData.m_start_time);
				magic.set_dispear_time(magicData.m_dispear_time);
				magic.set_distance(magicData.m_distance);
				
				return magic;
			}
		}
		return null;
	}
	
	public boolean initialize()
	{
		try
		{
			MySQLManager myman = AppContext.getManager(MySQLManager.class);
			ResultSet res = myman.executeSelect("SELECT id from magics");
			
			while(res.next())
			{
				String strId = res.getString("id");
				ResultSet magicRes = myman.executeSelect("SELECT * from magics WHERE id=" + strId);
				if(magicRes.next())
				{
					Integer id = new Integer(strId);
					Magic.MagicData magicData = new Magic.MagicData();
					magicData.m_id = id.intValue();
					magicData.m_type = magicRes.getByte("type");
					magicData.m_attack_power = magicRes.getInt("attack_power");
					magicData.m_start_time = magicRes.getFloat("start_time");
					magicData.m_dispear_time = magicRes.getFloat("dispear_time");
					magicData.m_distance = magicRes.getFloat("distance");
					this.magicMap.put(id, magicData);
				}
			}
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, "{0}", e);
		}
		return true;
	}
}
